package com.koron.system.userOrg.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.query.PageQuery;
import com.koron.system.userOrg.bean.po.AppUserOrgPo;
import com.koron.system.userOrg.mapper.AppUserOrgMapper;
import com.koron.system.userOrg.service.AppUserOrgService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (AppUserOrg)表服务实现类
 *
 * @author zhouj
 * @since 2021-11-22 09:11:09
 */
@Service("appUserOrgService")
public class AppUserOrgServiceImpl implements AppUserOrgService {

    @Autowired
    AppUserOrgMapper mapper;

    @Override
    public int insertOrUpdate(AppUserOrgPo appUserOrg) {
        return mapper.insertOrUpdate(appUserOrg);
    }

    @Override
    public int insertOrUpdateBatch(List<AppUserOrgPo> appUserOrgList) {
        if (CollectionUtils.isEmpty(appUserOrgList)) {
            return 0;
        }
        return mapper.insertOrUpdateBatch(appUserOrgList);
    }

    @Override
    public int insert(AppUserOrgPo appUserOrg) {

        return mapper.insert(appUserOrg);
    }

    @Override
    public int insert(String account, String orgId) {

        if (StringUtils.isEmpty(orgId)) {
            return 0;
        }

        List<AppUserOrgPo> userOrgPos = deal(account, orgId);

        return insertBatch(userOrgPos);
    }

    @Override
    public int insertBatch(List<AppUserOrgPo> appUserOrgList) {
        if (CollectionUtils.isEmpty(appUserOrgList)) {
            return 0;
        }
        return mapper.insertBatch(appUserOrgList);
    }

    @Override
    public int update(AppUserOrgPo appUserOrg) {
        return mapper.update(appUserOrg);
    }

    @Override
    public int updateBatch(List<AppUserOrgPo> appUserOrgPoList) {
        if (CollectionUtils.isEmpty(appUserOrgPoList)) {
            return 0;
        }
        String account = appUserOrgPoList.get(0).getAccount();
        deleteByAccount(account);
        return insertBatch(appUserOrgPoList);
    }

    @Override
    public int update(String account, String orgId) {

        if (StringUtils.isEmpty(orgId)) {
            return 0;
        }

        List<AppUserOrgPo> userOrgPos = deal(account, orgId);

        //先删除该账号下关联组织,再重新入库
        deleteByAccount(account);
        return insertBatch(userOrgPos);
    }

    public List<AppUserOrgPo> deal(String account, String orgId) {
        String[] split = orgId.split(",");
        List<String> orgIds = Arrays.asList(split);
        return orgIds.stream().map(p -> {
            AppUserOrgPo userOrgPo = new AppUserOrgPo();
            userOrgPo.setOrgId(p);
            userOrgPo.setAccount(account);
            return userOrgPo;
        }).collect(Collectors.toList());
    }

    @Override
    public int deleteById(String id) {
        return mapper.deleteById(id);
    }

    @Override
    public int deleteByAccount(String account) {
        return mapper.deleteByAccount(account);
    }

    @Override
    public int deleteByAccount(String account, String orgId) {
        return mapper.deleteByAccountOrg(account,orgId);
    }

    @Override
    public AppUserOrgPo queryById(String id) {

        return mapper.queryById(id);
    }

    @Override
    public List<AppUserOrgPo> queryList(AppUserOrgPo appUserOrg) {

        return mapper.queryList(appUserOrg);
    }

    @Override
    public List<AppUserOrgPo> queryByAccountList(List<String> accountList) {
        return mapper.queryByAccountList(accountList);
    }

    @Override
    public int deleteByAccountList(List<String> accountList,String orgId) {
        return mapper.deleteByAccountList(accountList,orgId);
    }

    @Override
    public PageInfo queryPageList(AppUserOrgPo appUserOrg, PageQuery pageQuery) {

        PageHelper.startPage(pageQuery.getPage(), pageQuery.getPageSize(), pageQuery.getOrderBy());
        List<AppUserOrgPo> list = mapper.queryList(appUserOrg);

        return new PageInfo(list);
    }

}
